package com.kaifamiao;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

public class XiaomiTest {

    public static void main(String[] args) {
        // 代理目标
        final Object target = new Foxconn();
        final Class<?> c = target.getClass();
        final ClassLoader loader = c.getClassLoader();
        final Class<?>[] interfaces = c.getInterfaces();

        InvocationHandler handler = (proxy,method,params) -> {
            System.out.println( "方法" + method.getName() + "即将执行" );
            // 通过反射方式调用 target 的 method 方法
            Object result = method.invoke( target, params );
            System.out.println( "方法"+ method.getName() + "执行后返回了" + result );
            // 将 method 方法的返回值继续返回
            return result;
        };

        // 代理对象
        Object proxy = Proxy.newProxyInstance( loader, interfaces, handler );

        System.out.println( proxy.toString() );
        System.out.println( proxy.hashCode() );
        System.out.println( proxy.equals(target) );

        if( proxy instanceof Xiaomi ) {
            Xiaomi x = (Xiaomi) proxy;
            Phone p = x.produce();
            System.out.println( p );
        }

    }

}
